package com.xh.audit.controller;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.Regulation;
import com.xh.audit.service.RegulationService;
import com.xh.audit.vo.webform.RegulationQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 控制层。
 * 规章制度
 *
 * @author lhl
 * @since 2023-10-31
 */
@RestController
@RequestMapping("/regulation")
@RequiredArgsConstructor
@Validated
public class RegulationController {
    private final RegulationService regulationService;

    @PostMapping("/save")
    public R<Regulation> save(@RequestBody
                              @Validated
                              Regulation tosave) {

        boolean saveFlag = regulationService.saveOrUpdate(tosave);
        if (saveFlag) {
            return R.success(regulationService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    /**
     * 根据主键删除。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("/del")
    public R<Regulation> del(@RequestBody
                             @Validated
                             @NotEmpty(message = "ids 集合不能空")
                             List<Integer> ids) {

        regulationService.removeByIds(ids);
        return R.success("删除成功");
    }


    /**
     * 分页查询。
     *
     * @param queryPage 分页对象
     * @return 分页对象
     */
    @PostMapping("/queryByPage")
    public R<Page<Regulation>> queryByPage(@RequestBody
                                           @Validated
                                           RegulationQuery queryPage) {

        Page<Regulation> page1 = regulationService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

}
